package ks.client.controllers.requests;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import ks.client.UserContext;
import ks.client.gui.UMGUI3;
import ks.client.lobby.Lobby;
import ks.framework.common.Message;
import ks.framework.interfaces.IProcessMessage;
import ks.managers.user.User;
import ks.managers.user.UserManager;

/**
 * The controller for the kicked request.
 * 
 * @author Christopher Page
 */
public class KickedController implements IProcessMessage {

	/** In case we need to process something. */
	Lobby lobby;
	
	/** Construct controller with lobby. */
	public KickedController(Lobby lobby) {
		this.lobby = lobby;
	}

	/**
	 * Client has received kicked message. Process it.
     * 
	 * @param m The message to process.
	 * @return
	 */
	public boolean process(Message m) {
		// Get context
		UserContext context = lobby.getUserContext();
		
		// get UserManager instance
		UserManager um = context.getUserManager();
		
		UMGUI3 gui = lobby.getUMGUI3();
		
		// extract information from the message
		Node kicked = m.contents();
		
		if (kicked == null)
			return false;
		
		NamedNodeMap nnpTmp = kicked.getAttributes();
		Node player = nnpTmp.getNamedItem("player");
		
		if (player == null)
			return false;
		
		User u = um.getUser(player.getNodeValue());
		
		if (u == null)
			return false;
		
		um.removeUser(u);
		gui.removeUser(u);
		
		return true;
	}
}
